home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch1 / Swin.ctl < prev    next >
Text File  |  1999-04-17  |  5KB  |  180 lines

  1. VERSION 5.00
  2. Begin VB.UserControl ScrolledWindow 
  3.    BorderStyle     =   1  'Fixed Single
  4.    ClientHeight    =   2175
  5.    ClientLeft      =   0
  6.    ClientTop       =   0
  7.    ClientWidth     =   2310
  8.    ControlContainer=   -1  'True
  9.    ScaleHeight     =   2175
  10.    ScaleWidth      =   2310
  11.    ToolboxBitmap   =   "Swin.ctx":0000
  12.    Begin VB.HScrollBar hbar 
  13.       Height          =   255
  14.       Left            =   0
  15.       TabIndex        =   3
  16.       Top             =   1800
  17.       Width           =   1575
  18.    End
  19.    Begin VB.VScrollBar vbar 
  20.       Height          =   1335
  21.       Left            =   1800
  22.       TabIndex        =   2
  23.       Top             =   120
  24.       Width           =   255
  25.    End
  26.    Begin VB.PictureBox picOuter 
  27.       BorderStyle     =   0  'None
  28.       Height          =   1215
  29.       Left            =   120
  30.       ScaleHeight     =   1215
  31.       ScaleWidth      =   1455
  32.       TabIndex        =   0
  33.       Top             =   120
  34.       Width           =   1455
  35.       Begin VB.PictureBox picInner 
  36.          BorderStyle     =   0  'None
  37.          Height          =   735
  38.          Left            =   120
  39.          ScaleHeight     =   735
  40.          ScaleWidth      =   855
  41.          TabIndex        =   1
  42.          Top             =   120
  43.          Width           =   855
  44.       End
  45.    End
  46. End
  47. Attribute VB_Name = "ScrolledWindow"
  48. Attribute VB_GlobalNameSpace = False
  49. Attribute VB_Creatable = True
  50. Attribute VB_PredeclaredId = False
  51. Attribute VB_Exposed = True
  52. Option Explicit
  53.  
  54. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  55.  
  56. ' Reposition picInner.
  57. Private Sub hbar_Change()
  58.     picInner.Left = -hbar.Value
  59. End Sub
  60.  
  61. ' Reposition picInner.
  62. Private Sub hbar_Scroll()
  63.     picInner.Left = -hbar.Value
  64. End Sub
  65.  
  66. ' Reparent the contained controls into picInner
  67. ' and see how much room they need.
  68. Private Sub ReparentControls()
  69. Dim ctl As Control
  70. Dim xmax As Single
  71. Dim ymax As Single
  72. Dim need_wid As Single
  73. Dim need_hgt As Single
  74.  
  75.     ' Do nothing if no controls have been loaded.
  76.     If ContainedControls.Count = 0 Then Exit Sub
  77.  
  78.     For Each ctl In ContainedControls
  79.         SetParent ctl.hWnd, picInner.hWnd
  80.  
  81.         xmax = ctl.Left + ctl.Width
  82.         ymax = ctl.Top + ctl.Height
  83.         If need_wid < xmax Then need_wid = xmax
  84.         If need_hgt < ymax Then need_hgt = ymax
  85.     Next ctl
  86.  
  87.     ' Make picInner big enough to hold the controls.
  88.     picInner.Move 0, 0, need_wid, need_hgt
  89.  
  90.     ' Hide the borders on picInner and picOuter.
  91.     picOuter.BorderStyle = vbBSNone
  92.     picInner.BorderStyle = vbBSNone
  93. End Sub
  94.  
  95. Private Sub UserControl_Resize()
  96.     ' Hide the controls at design time.
  97.     If Not Ambient.UserMode Then
  98.         vbar.Visible = False
  99.         hbar.Visible = False
  100.         picInner.Visible = False
  101.         Exit Sub
  102.     End If
  103.  
  104.     ' Arrange the controls.
  105.     ArrangeControls
  106. End Sub
  107. ' Arrange the scroll bars.
  108. Private Sub ArrangeControls()
  109. Dim border_width As Single
  110. Dim got_wid As Single
  111. Dim got_hgt As Single
  112. Dim need_wid As Single
  113. Dim need_hgt As Single
  114. Dim need_hbar As Boolean
  115. Dim need_vbar As Boolean
  116.  
  117.     ' Reparent the controls.
  118.     ReparentControls
  119.  
  120.     ' See how much room we have and need.
  121.     border_width = picOuter.Width - picOuter.ScaleWidth
  122.     got_wid = ScaleWidth - border_width
  123.     got_hgt = ScaleHeight - border_width
  124.     need_wid = picInner.Width
  125.     need_hgt = picInner.Height
  126.  
  127.     ' See if we need the horizontal scroll bar.
  128.     If need_wid > got_wid Then
  129.         need_hbar = True
  130.         got_hgt = got_hgt - hbar.Height
  131.     End If
  132.  
  133.     ' See if we need the vertical scroll bar.
  134.     If need_hgt > got_hgt Then
  135.         need_vbar = True
  136.         got_wid = got_wid - vbar.Width
  137.  
  138.         ' See if we now need the horizontal scroll bar.
  139.         If (Not need_hbar) And need_wid > got_wid Then
  140.             need_hbar = True
  141.             got_hgt = got_hgt - hbar.Height
  142.         End If
  143.     End If
  144.  
  145.     ' Arrange the controls.
  146.     picOuter.Move 0, 0, got_wid + border_width, got_hgt + border_width
  147.     If need_hbar Then
  148.         hbar.Move 0, got_hgt + border_width, got_wid + border_width
  149.         hbar.Min = 0
  150.         hbar.Max = picInner.ScaleWidth - got_wid
  151.         hbar.SmallChange = got_wid / 5
  152.         hbar.LargeChange = got_wid
  153.         hbar.Visible = True
  154.     Else
  155.         hbar.Value = 0
  156.         hbar.Visible = False
  157.     End If
  158.     If need_vbar Then
  159.         vbar.Move got_wid + border_width, 0, vbar.Width, got_hgt + border_width
  160.         vbar.Min = 0
  161.         vbar.Max = picInner.ScaleHeight - got_hgt
  162.         vbar.SmallChange = got_hgt / 5
  163.         vbar.LargeChange = got_hgt
  164.         vbar.Visible = True
  165.     Else
  166.         vbar.Value = 0
  167.         vbar.Visible = False
  168.     End If
  169. End Sub
  170.  
  171. ' Reposition picInner.
  172. Private Sub vbar_Change()
  173.     picInner.Top = -vbar.Value
  174. End Sub
  175.  
  176. ' Reposition picInner.
  177. Private Sub vbar_Scroll()
  178.     picInner.Top = -vbar.Value
  179. End Sub
  180.